package com.rescue.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.rescue.constant.MessageConstant;
import com.rescue.constant.SysSettingConstant;
import com.rescue.exception.BaseException;
import com.rescue.forum.dto.ApplyDTO;
import com.rescue.forum.entity.RescueFile;
import com.rescue.forum.entity.RescueMan;
import com.rescue.forum.syssetting.ApplySet;
import com.rescue.mapper.RescueFileMapper;
import com.rescue.mapper.RescueManMapper;
import com.rescue.mapper.SysSettingMapper;
import com.rescue.mapper.UserMessageMapper;
import com.rescue.service.RescueManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RescueManServiceImpl implements RescueManService {
    @Autowired
    private RescueManMapper rescueManMapper;
    @Autowired
    private SysSettingMapper sysSettingMapper;
    @Autowired
    private RescueFileMapper rescueFileMapper;
    @Autowired
    private UserMessageMapper userMessageMapper;

    /**
     * 根据userId过去救援用户信息
     *
     * @param userId
     * @return
     */
    public RescueMan getById(Integer userId) {
        return rescueManMapper.getById(userId);
    }


    /**
     * 修改用户信息
     *
     * @param rescueMan
     */
    public void update(RescueMan rescueMan) {
        rescueManMapper.update(rescueMan);
    }


    /**
     * 申请为救援者
     *
     * @param applyDTO
     */
    @Transactional
    public void apply(ApplyDTO applyDTO) {
        //如已为救援者不可申请
        RescueMan rescueMan = rescueManMapper.getById(applyDTO.getUserId());
        if (rescueMan == null) {
            throw new BaseException(MessageConstant.ORDER_NOT_FOUND);
        }
        if (rescueMan.getIsRescueMan() == 1) {
            throw new BaseException(MessageConstant.ALREADY_RESCUER);
        }
        //将申请材料保存
        String imgPaths = applyDTO.getImgPaths();
        List<RescueFile> list = new ArrayList<>();

        List<String> imgList = JSONObject.parseArray(imgPaths, String.class);
        if (imgList != null && imgList.size() != 0) {
            imgList.forEach(s -> list.add(RescueFile.builder()
                    .status(1)
                    .userId(applyDTO.getUserId())
                    .type(0)
                    .filePath(s).build()));
        }
        List<String> fileList = JSONObject.parseArray(applyDTO.getFilePaths(), String.class);
        if (fileList != null && fileList.size() != 0) {
            fileList.forEach(s -> list.add(RescueFile.builder()
                    .status(1)
                    .userId(applyDTO.getUserId())
                    .type(3)
                    .filePath(s).build()));

        }
        //将list批量添加到救援文件表
        if (list.size() != 0) {
            rescueFileMapper.saveBatch(list);
        }

        //判断是否需要审核
        ApplySet applySet = JSONObject.parseObject(sysSettingMapper.getByCode(SysSettingConstant.APPLY_SETTING), ApplySet.class);

        if (applySet.getRescueAudit()) {
            //需审核
            rescueManMapper.update(RescueMan.builder()
                    .userId(applyDTO.getUserId())
                    .isRescueMan(2).build());
            //发送消息

        } else {
            //不需审核
            rescueManMapper.update(RescueMan.builder()
                    .userId(applyDTO.getUserId())
                    .isRescueMan(1).build());
            //发送消息
        }
    }
}
